package com.cilico.components;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Rect;
import android.graphics.RectF;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;

import com.cilico.R;
import com.cilico.utils.ConstantRepository;
import com.cilico.utils.Utils;
import com.cilico.view.BaseView;
import com.cilico.view.FormLayout;

/**
 * Div控件
 * @author lv.xiangan
 *
 */
public class Div extends LinearLayout implements Panel {
	private static final int PADDING = 3;
	private static final int VPADDING = 5;
	public CssStyle cssStyle_;
	private ArrayList<Component> children_;
	private Paint paint_;
	private RectF rf_;
	public String display_; // CSS style of "display".
	public String name_; // name of the DIV.
	public String strHeight_;
	private int backgroundColor_; // background color to paint.
	protected Table parentTable_; // indicate the table by which the layout was created.
	private BaseView bv_;
	private Bitmap backgroundImg_;
	private LinearLayout selfLayout_;
	private boolean isInTable_;

	private int width_;
	public int height_;
	private FormLayout formllayout_;

	/**
	 * @param context
	 */
	public Div(Context context) {
		super(context);
		bv_ = (BaseView) context;
		setBackgroundColor(0);
		LayoutParams lop = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		setLayoutParams(lop);
		paint_ = null;
		paint_ = new Paint();
		paint_.setStyle(Style.FILL);
		paint_.setColor(0xff000000);
		rf_ = new RectF();
	}

	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		if (backgroundColor_ != 0) {
			int connerRadius_ = 5;
			paint_.setColor(backgroundColor_);
			canvas.drawRoundRect(rf_, connerRadius_, connerRadius_, paint_);
		}
		if (backgroundImg_ != null) {
			canvas.drawBitmap(backgroundImg_, 0, 0, paint_);
		}
	}

	public void addView(View child) {
		try {
			Component c = (Component) child;
			c.mould();
			super.addView(c.getCmpView());
		} catch (Exception e) {
			Utils.printException(e);
		}
	}

	public void setBackgroundColor(int color) {
		backgroundColor_ = color;
	}

	public void cleanText() {
		// TODO Auto-generated method stub
	}

	public String getContentText() {
		// TODO Auto-generated method stub
		return null;
	}

	public void mould() {
		CssStyle cs = cssStyle_;
		if (cs != null) {
			if (cs.bgColor_ != 0) {
				setBackgroundColor(cs.bgColor_);
			}
			if (cs.display_ != null) {
				display_ = cs.display_;
			}
			LinearLayout.LayoutParams dlp = (LayoutParams) getLayoutParams();
			if (dlp == null) {
				dlp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
			}
			int w = 0;
			int h = 0;
			for (int i = 0; i < getChildCount(); i++) {
				View v = getChildAt(i);
				try {
					if (v instanceof SelfLayout) {
						w += ((SelfLayout) v).getRealWidth();
						h += ((SelfLayout) v).getRealHeight();
					} else {
						int wd = ((Component) v).getCmpWidth();
						h += ((Component) v).getCmpHeight();
						try {
							LayoutParams lp = (LinearLayout.LayoutParams) v.getLayoutParams();
							wd += lp.leftMargin + lp.rightMargin;
							h += lp.topMargin + lp.bottomMargin;
						} catch (Exception e) {
							Utils.printException(e);
						}
						w = Math.max(w, wd);
					}
				} catch (Exception e) {
					Utils.printException(e);
				}
			}
			if (w > 0) {
				dlp.width = w;
			}
			if (h > 0) {
				dlp.height = h;
			}
			if (cs.width_ > 0) {
				dlp.width = cs.width_;
			}
			if (cs.height_ > 0) {
				dlp.height = cs.height_;
			}
			if (dlp.width < Utils.screenWidth_) {
				dlp.width += 2 * PADDING;
				dlp.height += 2 * VPADDING;
				setPadding(PADDING, VPADDING, PADDING, VPADDING);
			}
			if (selfLayout_ != null) {
				BaseView.removeViewsStepByStep(selfLayout_, this);
			}
			selfLayout_ = BaseView.createSelfLayout(bv_, dlp, cssStyle_, this);
			setLayoutParams(dlp);
			width_ = dlp.width;
			height_ = dlp.height;
			if (cs.height_ < 0) {
				dlp.height = LayoutParams.WRAP_CONTENT;
			}
		}
		setGravity(Gravity.CENTER);
		setTableBgImg(width_, height_);
		rf_.set(new Rect(0, 0, width_, height_));
		super.setBackgroundColor(0);
	}


	private void setTableBgImg(int width, int height) {
		CssStyle cs = cssStyle_;
		Bitmap bgImg = null;
		if (cs != null && cs.bgImageUrl_ != null) {
			if (cs.bgImageUrl_.startsWith("local:")) {
				String imgName = cs.bgImageUrl_.substring("local:".length());
				if (imgName.equals("divBg.png")) {
					bgImg = ConstantRepository.decodeBitmap(bv_.getResources(), R.drawable.divbg);
					backgroundImg_ = Bitmap.createScaledBitmap(bgImg, width, height, true);
					if (bgImg != null && !bgImg.isRecycled()) {
						bgImg.recycle();
					}
					bgImg = null;
				}
			}
		}
	}

	public void setContentText(String text) {
		// TODO Auto-generated method stub
	}

	public void shrinkWidth() {
		// TODO Auto-generated method stub
	}

	public void setCmpHeidht(int height) {
		// TODO Auto-generated method stub
	}

	public void setCmpWidth(int width) {
		// TODO Auto-generated method stub
	}

	public void setCssStyle(CssStyle style) {
		cssStyle_ = style;
	}

	public View getCmpView() {
		if (selfLayout_ != null) {
			return selfLayout_;
		}
		return this;
	}

	public void addChild(Component cmp) {
		if (children_ == null) {
			children_ = new ArrayList<Component>();
		}
		children_.add(cmp);
	}

	public void loadAllChildren() {
		if (children_ == null) {
			return;
		}
		BaseView.appendChildIntoLayout(bv_, this, children_, 0);
	}

	public int childrenAmount() {
		if (children_ != null) {
			return children_.size();
		}
		return 0;
	}

	public Component child(int index) {
		try {
			return children_.get(index);
		} catch (Exception e) {
			Utils.printException(e);
		}
		return null;
	}

	public void removeChild(Component cmp) {
		children_.remove(cmp);
	}

	public int getCmpHeight() {
		return height_;
	}

	public int getCmpWidth() {
		return width_;
	}

	public void setInTable(boolean inTable) {
		isInTable_ = inTable;
	}

	public boolean isInTable() {
		return isInTable_;
	}

	public CssStyle getCssStyle() {
		return cssStyle_;
	}

	public void releaseResource(View view) {
		if (null != backgroundImg_ && !backgroundImg_.isRecycled()) {
			backgroundImg_.recycle();
		}
		backgroundImg_ = null;

		Utils.releaseResource(view);
	}

	public void setForm(FormLayout formLayout) {
		formllayout_ = formLayout;
	}

	public FormLayout getForm() {
		return formllayout_;
	}

	@Override
	public void setEncrypt(String encrypt) {

	}
}